﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using ConsoleApplication.BLL;
using ConsoleApplication.Model;

namespace ConsoleApplication
{

    class ReadXml
    {
        private static readonly string strPath = @"E:\Files\works\MyCodeShop\my-codeshop\MyWebSite\Data\XML\XmlConfig.xml";

        [STAThread]
        public static void ReadXmlMain()
        {
            ReadXmlFile();
            EditParamValue2Xml();

            //string[] AttributeNames = { "Name", "Value" };
            //string[] AttributeValues = { "111", "222" };
            //CreateNewXmlNode(strPath, "Parameter", AttributeNames, AttributeValues);
        }      

        public ReadXml()
        { }

        private static void ReadXmlFile()
        {
            string s = ParameterSerialize.ReadFile(strPath);
            Parameter[] Parameters = ParameterSerialize.DeSerializeParameters(s);
            foreach (Parameter param in Parameters)
            {
                Console.WriteLine(param.Name);
            }
            Console.ReadKey();

            SearchXmlNode(strPath, "111");
            Console.ReadKey();
        }

        private static void EditParamValue2Xml()
        { 
            XmlDocument doc = new XmlDocument();
            doc.Load(strPath);
            XmlElement element = doc.DocumentElement;
            if (element is XmlNode && element.HasChildNodes)
            {
                foreach(XmlNode node in element.ChildNodes)
                {
                    if (node.Attributes["Name"].Value == "ScrollTime")
                    {
                        node.Attributes["Value"].Value = "5000";
                        doc.Save(strPath);
                    }
                }
            }
        }

        /// <summary>
        /// Create a new XmlNode under the ROOT Node
        /// </summary>
        /// <param name="XmlPath"></param>
        /// <param name="nodeName"></param>
        /// <param name="AttributeNames"></param>
        /// <param name="AttributeValues"></param>
        public static void CreateNewXmlNode(string XmlPath, string nodeName, string[] AttributeNames, string[] AttributeValues)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(XmlPath);
            XmlElement element = doc.DocumentElement;
            if (element is XmlNode)
            {
                for (int i = 0; i < 10;i++ )
                    element.AppendChild(GenerateXmlNode(element, nodeName, AttributeNames, AttributeValues));
                doc.Save(XmlPath);
            }
        }
        /// <summary>
        /// Generate a new XmlNode under the parameter Node
        /// </summary>
        /// <param name="Node"></param>
        /// <param name="nodeName"></param>
        /// <param name="AttributeNames"></param>
        /// <param name="AttributeValues"></param>
        /// <returns>the new Node</returns>
        public static XmlNode GenerateXmlNode(XmlNode Node, string nodeName, string[] AttributeNames, string[] AttributeValues)
        {
            if (Node is XmlNode)
            {
                XmlElement newE = Node.OwnerDocument.CreateElement(nodeName);
                for (int i = 0; i < AttributeNames.Length; i++)
                    newE.SetAttribute(AttributeNames[i], AttributeValues[i]);

                return newE;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        ///   use xPath to select nodeList, and deleted them all.
        /// </summary>
        /// <param name="XmlPath"></param>
        /// <param name="nodeName"></param>
        public static void SearchXmlNode(string XmlPath, string AttributeName)
        {
            XmlNodeList list = GetXmlNodeList(XmlPath, string.Format(@"Parameter[@Name='{0}']", AttributeName));
            foreach (XmlNode node in list)
            {
                Console.WriteLine(string.Format(@"<{0}-{1}={2}> has been removed.", node.Name, node.Attributes[0].Name, node.Attributes[0].Value));
                RemoveXmlNode(node);
            }
        }
        /// <summary>
        /// Get NodeList with xPath
        /// </summary>
        /// <param name="XmlPath"></param>
        /// <param name="xPath"></param>
        /// <returns></returns>
        public static XmlNodeList GetXmlNodeList(string XmlPath, string xPath)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(XmlPath);
            XmlElement element = doc.DocumentElement;
            if (element is XmlNode)
            {
                return element.SelectNodes(xPath);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Remove an specific Node
        /// </summary>
        /// <param name="Node"></param>
        /// <returns></returns>
        public static bool RemoveXmlNode(XmlNode Node)
        {
            if (Node is XmlNode)
            {
                string baseURI = Node.BaseURI;
                string namespaceURI = Node.NamespaceURI;        //  string.Empty
                Node.ParentNode.RemoveChild(Node);
                Node.OwnerDocument.Save(baseURI.Replace("file:///", ""));
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 递归查找节点
        /// </summary>
        /// <param name="node">起始节点</param>
        /// <param name="id">attr</param>
        private static void GetXmlDoc(XmlNode node, string id, ref XmlNode myNode)
        {
            foreach (XmlNode xml in node.ChildNodes)
            {
                if (xml.Attributes["id"].Value == id)
                    myNode = xml;
                else
                    GetXmlDoc(xml, id, ref myNode);
            }
        }
    }
}
